package com.learn.hibernatevalidator.chapter06.constraint;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * @author: liyf
 * @create: 2025-01-09 22:47
 **/
public class CheckCaseValidator implements ConstraintValidator<CheckCase, String> {

    private CaseMode caseMode;

    @Override
    public void initialize(CheckCase constraintAnnotation) {
        this.caseMode = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }

        if (caseMode == CaseMode.UPPER) {
            return value.toUpperCase().equals(value);
        } else {
            return value.toLowerCase().equals(value);
        }
    }
}
